//传递函数相关的一些内容

var g_tf_pointCtrl = new Array(0);                  //控制点列表
var g_tf_pointColor = new Array(256);               //十六进制三位的颜色
var g_tf_pointOpacity = new Uint8Array(256);        //十进制透明度（0到255）


function initTFVars()
{
    for(var i=0;i<256;i++)
    {
        g_tf_pointColor[i] = "#ffffff";
        g_tf_pointOpacity[i] = 0;
    }
    g_tf_pointCtrl.push(0);
    addTFCtrlBtn(0);
    g_tf_pointCtrl.push(255);
    addTFCtrlBtn(255);
}

function ReCalculateTFTex()     //重新计算传递函数纹理
{
    for(var i=0;i<256;i++)
    {
        if(judgeCtrlPoint(i))
            continue;

        var n = 1;
        for (; n < g_tf_pointCtrl.length; n++)
            if (i <= g_tf_pointCtrl[n])
                break;
        var k = (i - g_tf_pointCtrl[n - 1]) / (g_tf_pointCtrl[n] - g_tf_pointCtrl[n - 1]);

        //插值计算灰度值
        g_tf_pointOpacity[i] = (g_tf_pointOpacity[g_tf_pointCtrl[n - 1]] * (1.0 - k)) + (g_tf_pointOpacity[g_tf_pointCtrl[n]] * k);

        //插值计算颜色（需要先把十六进制的字符串转换为十进制的三个数）
        var colorL = colorh2d(g_tf_pointColor[g_tf_pointCtrl[n-1]]);
        var colorR = colorh2d(g_tf_pointColor[g_tf_pointCtrl[n]]);
        var color = new Array(3);
        for(var j=0;j<3;j++)
            color[j] = colorL[j]*(1.0-k)+colorR[j]*k;
        g_tf_pointColor[i] = colord2h(color[0],color[1],color[2]);
    }
    applyToShaderTFTex();
}

function applyToShaderTFTex()       //将变化应用到着色器中的传递函数纹理中
{
    var data = new Uint8Array(256 * 4 * 4);

    for (var y = 0; y < 4; y++)             //高是4
        for (var x = 0; x < 256; x++)       //宽是256
        {
            var color = colorh2d(g_tf_pointColor[x]);
            data[(y * 256 + x) * 4 + 0] = color[0];                 //红
            data[(y * 256 + x) * 4 + 1] = color[1];                 //绿
            data[(y * 256 + x) * 4 + 2] = color[2];                 //蓝
            data[(y * 256 + x) * 4 + 3] = g_tf_pointOpacity[x];  //不透明度
        }

    var gl = getGLFromDoc();
    var tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);


    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 4, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
    gl.generateMipmap(gl.TEXTURE_2D);
    gl.bindTexture(gl.TEXTURE_2D, null);
    g_tex_TransFunc = tex;
}

function ReDrawTFTex()      //重新绘制传递函数纹理
{
    var canvas = document.getElementById("TransFuncTexCanvas")
    var context = canvas.getContext("2d");

    var canvasHeight = canvas.height;
    canvas.width = g_HBWidth*256;

    for(var i=0;i<256;i++)
    {
        context.fillStyle=colord2h(g_tf_pointOpacity[i],g_tf_pointOpacity[i],g_tf_pointOpacity[i]);
        context.fillRect(i*g_HBWidth, 0, g_HBWidth, 25);
        context.fillStyle=g_tf_pointColor[i];
        context.fillRect(i*g_HBWidth, 25, g_HBWidth,67);
    }
}

function judgeCtrlPoint(pos)        //判断pos位置上是否有控制点
{
    for(var i=0;i<g_tf_pointCtrl.length;i++)
    {
        if(g_tf_pointCtrl[i]==pos)
        return true;
    }
    return false;
}

function addTFCtrlPoint(pos)        //添加一个控制点在pos位置上
{
    for(var i=0;i<g_tf_pointCtrl.length;i++)
    {
        if(g_tf_pointCtrl[i]==pos)
            return;
        if(g_tf_pointCtrl[i]>pos)
        {
            g_tf_pointCtrl.splice(i,0,pos);
            break;
        }
    }


    addTFCtrlBtn(pos);
    ui_TF_setpos(pos);
}

function delTFCtrlPoint(pos)
{
    if(judgeCtrlPoint(pos)==false)
        return;
    for(var i=0;i<g_tf_pointCtrl.length;i++)
    {
        if(g_tf_pointCtrl[i]==pos)
        {
            g_tf_pointCtrl.splice(i,1);
            break;
        }
    }

    ReCalculateTFTex();
    ReDrawTFTex();

    ui_TF_setpos(pos);

    document.getElementById("tf_point"+pos).remove();
}

function addTFCtrlBtn(pos)          //添加控制点的按钮
{
    var newPoint = document.createElement("input");//输入想要创建的类型
    newPoint.id="tf_point"+pos;
    newPoint.type = "button";
    newPoint.style.position = "absolute";
    var width = g_HBWidth*1.5;
    if(width<20)width=20;
    newPoint.style.width = ""+width+"px";
    newPoint.style.height = "20px";
    newPoint.style.top = "200px";
    newPoint.style.left=""+(pos*g_HBWidth + g_HBWidth/2-width/2)+"px";
    newPoint.onclick=function(){ui_TF_setpos(pos);};//"ui_TF_setpos("+ pos +")"
    document.getElementById("BtmMidDiv").appendChild(newPoint);
}

function ResetTFBtnPos()        //重设控制点按钮的位置和大小
{
    for(var i=0;i<g_tf_pointCtrl.length;i++)
    {
        var pos = g_tf_pointCtrl[i];
        var point = document.getElementById("tf_point"+pos);
        var width = g_HBWidth*1.5;
        if(width<20)width=20;
        point.style.width = ""+width+"px";
        point.style.left=""+(pos*g_HBWidth + g_HBWidth/2-width/2)+"px";
    }
}